﻿

using Ccteg.Common;
using Ccteg.Common.QueryModels.DncPermission;

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;

namespace Ccteg.DB
{
    /// <summary>
    /// 
    /// </summary>
    public class MySqlDbContext : DbContext
    {


        /// <summary>
        /// 
        /// </summary>
        /// <param name="options"></param>
        public MySqlDbContext(DbContextOptions<MySqlDbContext> options) : base(options)
        {

        }
        /// <summary>
       

        /// <summary>
        /// 事故隐患排查
        /// </summary>
        public DbSet<Htc_Trouble> Htc_Trouble { get; set; }

        /// <summary>
        /// EF变更日志
        /// </summary>
        private void WriteEFDataLog()
        {
            //获取到EF变更条目
            var list = this.ChangeTracker.Entries();
            foreach (var item in list)
            {
                //对应的表名
                string tableName = "";

                #region 获取表名
                Type type = item.Entity.GetType();
                Type patientMngAttrType = typeof(TableAttribute);
                TableAttribute attribute = null;
                if (type.IsDefined(patientMngAttrType, true))
                {
                    attribute = type.GetCustomAttributes(patientMngAttrType, true).FirstOrDefault() as TableAttribute;
                    if (attribute != null)
                    {
                        tableName = attribute.Name;
                    }
                }

                if (string.IsNullOrEmpty(tableName))
                {
                    tableName = type.Name;
                }
                #endregion

                switch (item.State)
                {
                    case EntityState.Detached:

                        break;
                    case EntityState.Unchanged:

                        break;
                    case EntityState.Deleted:

                        break;
                    case EntityState.Modified:
                        // WriteEFUpdateLog(item, tableName);
                        var item1 = item;
                        var cc = tableName;
                        break;
                    case EntityState.Added:

                        break;
                }
            }
        }

        //public override int SaveChanges(bool efLogFlag=false)
        //{


        //    if (efLogFlag)
        //    {
        //        WriteEFDataLog();
        //    }

        //    return base.SaveChanges();
        //}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Scf_Scf>(e => { e.HasQueryFilter(x => x.Deleted == false); });
            modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
            base.OnModelCreating(modelBuilder);

        }
    }
}
